package com.ysy.tz.mfg.controller;

import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.event.LoginEvent;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.utils.FileResourceLoader;
import com.ysy.tz.core.utils.FormUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.entity.FlightsEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.service.UserService;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;

public class LoginController implements Initializable {
    @FXML
    private ImageView leftImage;
    @FXML
    private ComboBox flightsCode;
    @FXML
    private DatePicker workDate;
    @FXML
    private VBox loginForm;
    @Service
    UserService userService;

    public LoginController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        try {
            this.leftImage.setImage(new Image(FileResourceLoader.loadInputStream("/views/images/login/lgbg2.png")));
            this.leftImage.setFitWidth(0.415625 * Screen.getPrimary().getVisualBounds().getWidth());
            this.leftImage.setFitHeight(Screen.getPrimary().getVisualBounds().getHeight());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        ArrayList<FlightsEntity> flightsList = (ArrayList)SessionUtils.getAttribute(SessionUtils.FLIGHTS);
        this.flightsCode.setItems(FXCollections.observableArrayList(flightsList));
        this.flightsCode.getSelectionModel().selectFirst();
        this.workDate.setValue(LocalDate.now());
    }

    public void close() {
        System.exit(0);
    }

    public void login() {
        try {
            UserEntity userEntity = (UserEntity)FormUtils.collectParams(this.loginForm, UserEntity.class);
            UserEntity target = this.userService.login(userEntity);
            if (target != null) {
                target.setFlightsCode(userEntity.getFlightsCode());
                ApplicationContext.eventBus.post(new LoginEvent(true, "login success!"));
                ApplicationContext.loginDialog.close();
                ApplicationContext.loginDialog = null;
            } else {
                CustomerAlert alert = new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("code_name_error"), new ButtonType[0]);
                alert.showAndWait();
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }
}
